package com.richapp.launcher.scene.actions;

import com.jack.util.date.DateUtil;
import com.jack.util.string.StringUtil;
import com.richapp.launcher.database.system.dao.BoxModelDao;
import com.richapp.launcher.database.system.dao.UserMachineDao;
import com.richapp.launcher.database.system.mode.BoxModel;
import com.richapp.launcher.database.system.mode.UserMachine;
import com.richapp.launcher.database.system.mode.UserMachineButton;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractInfraredAction;
import com.richapp.launcher.serialport.gateway.command.InfraredControlCmd;
import com.richapp.launcher.serialport.infrared.InfraredServer;
import com.richapp.launcher.serialport.infrared.message.ControlMessage;
import com.richapp.net.util.HexTool;

/**
 * Created by jack on 2015/6/4 0004.
 */
public class SceneInfraredAction extends SceneAbstractInfraredAction {

    @Override
    public int getActionType() {
        return SCENE_ACTION_TYPE_INFRARED;
    }

    @Override
    public void doAction() {
        UserMachineButton userMachineButton = getMacineButton();
        if(userMachineButton != null){
            if(userMachineButton.getCode() == null || "".equals(userMachineButton.getCode().trim())){
                return;
            }
            BoxModel currentBox = new BoxModelDao(SceneServer.getInstance().getContext()).getCurrentBox();
            UserMachine userMachine = new UserMachineDao(SceneServer.getInstance().getContext()).get(currentBox.getUserName(),getMachineName());

            byte[] controlData = HexTool.HexToByteArr(userMachineButton.getCode());
            if(!StringUtil.isNullOrEmpty(userMachine.getBindBoxCpuId(),userMachine.getBindDeviceId(),userMachine.getBindDeviceNumber(),userMachine.getBindWay())){
                if(Boolean.parseBoolean(userMachineButton.getStudyFlag())){
                    InfraredControlCmd.getInstance().controlCmd(userMachine.getBindBoxCpuId(),userMachine.getBindDeviceId(),userMachine.getBindDeviceNumber(),userMachine.getBindWay(),controlData, DateUtil.getDateLong()+"");
                }else{
                    int length = controlData.length + 2;
                    String lengthStr;
                    if(length > 255){
                        lengthStr = Integer.toHexString(length);
                    }else{
                        lengthStr = "00" + Integer.toHexString(length);
                    }
                    InfraredControlCmd.getInstance().controlCmd2(userMachine.getBindBoxCpuId(),userMachine.getBindDeviceId(),userMachine.getBindDeviceNumber(),userMachine.getBindWay(),controlData, DateUtil.getDateLong()+"",HexTool.HexToByteArr(lengthStr),(byte)userMachine.getMachineTypeId());
                }
            }else{
                int deviceId = 0x00;
                if("true".equals(userMachineButton.getStudyFlag())){
                    deviceId = 0x30;
                }

                ControlMessage controlMessage = new ControlMessage();
                controlMessage.setDeviceType((byte)userMachine.getMachineTypeId());
                controlMessage.setDeviceId((byte) deviceId);
                controlMessage.setControlData(controlData);
                controlMessage.setUserName(currentBox.getUserName());
                controlMessage.setControlKey(System.currentTimeMillis());
                int length = controlData.length + 2;
                String lengthStr;
                if(length > 255){
                    lengthStr = Integer.toHexString(length);
                }else{
                    lengthStr = "00" + Integer.toHexString(length);
                }
                controlMessage.setLength(HexTool.HexToByteArr(lengthStr));
                InfraredServer.getInstance().send(controlMessage);
            }
        }
    }
}
